package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    /**
     * 根据手机号查询会员信息
     * @param telephone
     * @return
     */
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }

    /**
     * 添加会员
     * @param member
     */
    public void add(Member member) {

        //如果用户设置了密码，需要对密码进行md5加密
        String password = member.getPassword();
        if(password != null){
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }else {
            //设置默认密码123456
            member.setPassword(MD5Utils.md5("123456"));
        }
        //设置注册时间
        member.setRegTime(new Date());

        memberDao.add(member);
    }

    /**
     * 根据月份查询会员数量
     * @param months
     * @return
     */
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCounts = new ArrayList<>();
        if(months != null && months.size() > 0){
            for (String month : months) {//2019.01
                String endTime = month + ".31";
                Integer memberCount = memberDao.findMemberCountBeforeDate(endTime);
                memberCounts.add(memberCount);
            }
        }
        return memberCounts;
    }

    /**
     * 分页查询会员管理列表
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        //获取表单的当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        //获取表单的单页数量
        Integer pageSize = queryPageBean.getPageSize();
        //查询条件
        String queryString = queryPageBean.getQueryString();//ThreadLocal
        PageHelper.startPage(currentPage,pageSize);//分页插件，会在执行sql之前将分页关键字追加到SQL后面
        //忽略空格查询条件
        if(queryString != null && queryString.contains(" ")){
            queryString = queryString.replaceAll(" ", "");
        }

        Page<Member> page = memberDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 用过id回显会员数据
     * @param id
     * @return
     */
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    /**
     * 编辑会员
     * @param member
     */
    public void edit(Member member) {
        memberDao.edit(member);
    }

    /**
     * 手机号唯一性验证
     * @param member
     * @return
     */
    public String phoneValidator(Member member) {
        //获取表单提交的手机号
        String phoneNumber = member.getPhoneNumber();
        //获取表单提交的id
        Integer id = member.getId();
        //查询当前手机号是否已经被注册
        Integer phone = memberDao.findByPhone(phoneNumber);
        //通过表单的id查询旗下手机号与表单提交的手机号进行对比
        Member byId = memberDao.findById(id);
        if(phone > 0 && id == null){
            return "手机号已经存在";
        }else if(id != null && !byId.getPhoneNumber().equals(phoneNumber) && phone != 0){
            return "手机号已经存在";
        }else {
            return "恭喜您通过验证";
        }


    }

    /**
     * 档案号唯一性验证
     * @param member
     * @return
     */
    public String fileNumberValidator(Member member) {
        //获取表单提交的档案号
        String fileNumber = member.getFileNumber();
        //获取表单提交的id
        Integer id = member.getId();
        //查询当前档案号已经被使用
        Integer byFileNumber = memberDao.findByFileNumber(fileNumber);
        //通过表单的id查询旗下档案号与表单提交的档案号进行对比
        Member byId = memberDao.findById(id);
        if(byFileNumber > 0 && id == null){
            return "档案号已经存在";
        }else if(id != null && !byId.getFileNumber().equals(fileNumber) && byFileNumber != 0){
            return "档案号已经存在";
        }else {
            return "恭喜您通过验证";
        }
    }

    /**
     * 身份证唯一性验证
     * @param member
     * @return
     */
    public String idCardValidator(Member member) {


        //获取表单提交的身份证号
        String idCard = member.getIdCard();
        //获取表单提交的id
        Integer id = member.getId();
        //查询当前身份证号是否已经被注册
        Integer byIdCard = memberDao.findByIdCard(idCard);
        //通过表单的id查询旗下身份证号与表单提交的身份证号进行对比
        Member byId = memberDao.findById(id);
        if(byIdCard > 0 && id == null){
            return "身份证已经存在";
        }else if(id != null && !byId.getIdCard().equals(idCard) && byIdCard != 0){
            return "身份证已经存在";
        }else {
            return "恭喜您通过验证";
        }
    }

    /**
     * 邮箱唯一性验证
     * @param member
     * @return
     */
    public String emailValidator(Member member) {

        //获取表单提交的邮箱号
        String email = member.getEmail();
        //获取表单提交的id
        Integer id = member.getId();
        //查询当前邮箱号是否已经被注册
        Integer byEmail = memberDao.findByEmail(email);
        //通过表单的id查询旗下邮箱号与表单提交的邮箱号进行对比
        Member byId = memberDao.findById(id);
        if(byEmail > 0 && id == null){
            return "邮箱已经存在";
        }else if(id != null && !byId.getEmail().equals(email) && byEmail != 0){
            return "邮箱已经存在";
        }else {
            return "恭喜您通过验证";
        }
    }

    /**
     * 分页查询体检上传列表
     * @param queryPageBean
     * @return
     */
    public PageResult findCheckPage(QueryPageBean queryPageBean) {

        //获取表单的当前页
        Integer currentPage = queryPageBean.getCurrentPage();
        //获取表单的单页数量
        Integer pageSize = queryPageBean.getPageSize();
        //查询条件
        String queryString = queryPageBean.getQueryString();//ThreadLocal
        PageHelper.startPage(currentPage,pageSize);//分页插件，会在执行sql之前将分页关键字追加到SQL后面
        //忽略空格查询条件
        if(queryString != null && queryString.contains(" ")){
            queryString = queryString.replaceAll(" ", "");
        }

        Page<Map> page = memberDao.findCheckByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 删除体检
     * @param id
     */
    public void deleteCheck(Integer id) {
        memberDao.deleteCheck(id);
    }
}
